package screen_calibration;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

/**
 *  This class holds the Spectrum chart object
 *  @author Alex Frid alex.frid@gmail.com; Ran Dror 
 **/
public class CanvasLabel extends JPanel implements ActionListener{
    
    /** Width of chromaticity diagram */
    public static final int WIDTH = 300;
    
    /** Height of chromaticity diagram */
    public static final int HEIGHT = 300;
    
    /** RGB coordinates. */
    public RGBCoord rgbLocal;
    
    /** Canvas that holds the chromaticity diagram. */
    public ChromaCanvas chroma;
    
    /** Three-color proportional mixer. */
    public Mixer mixer;
    
    /** Resulting RGB color component */
    public CompColor compColor;
    
    /** The Text field for the primaries results*/
    private JTextField[][] pMatField;
    
    /** Text field for the gamma*/
    private JTextField gamma;
    
    /** Add it to the UI for testing manualy the class correctness */
    private JLabel testLabel;
    
    // The data that collected from the user for performing the test
    private JTextField testTextField_x, testTextField_y;
    
    // Accept the test
    private JButton testButton;
    
    /** The c'tor of the class */
    public CanvasLabel(){
        setLayout(new BorderLayout());
        
        setSize(WIDTH,HEIGHT /*+60*/);
        setPreferredSize(new Dimension(WIDTH,HEIGHT));
        setMaximumSize(new Dimension(WIDTH,HEIGHT));
        setMinimumSize(new Dimension(WIDTH,HEIGHT));
        
        rgbLocal = new RGBCoord( 0, 0, 0 );
        
        chroma = new ChromaCanvas( this );
        
        chroma.resize( WIDTH, HEIGHT );	// resize()
        
        mixer = new Mixer( this );
        
        Dimension d = new Dimension(36,22);
        
        pMatField = new JTextField[3][3];
        
        gamma = new JTextField();
        gamma.setSize(d);
        gamma.setPreferredSize(d);
        gamma.setMaximumSize(d);
        gamma.setMinimumSize(d);
                
        compColor = new CompColor( rgbLocal );
        //compColor.setSize(d);
        //compColor.setPreferredSize(d);
        //compColor.setMaximumSize(d);
        //compColor.setMinimumSize(d);
        
        JPanel tempPanel = new JPanel(new GridLayout(2,1));
        
        JPanel sumPanel = new JPanel(new BorderLayout());
        
        JPanel colorPanel = new JPanel(new GridLayout(1,1));
        
        sumPanel.setBorder(BorderFactory.createCompoundBorder(
        		BorderFactory.createEmptyBorder(8, 0, 20, 20),
        		BorderFactory.createTitledBorder("Results")));
		
        addSumaryPanel(sumPanel);
        
        colorPanel.add(compColor);
        colorPanel.setBorder(BorderFactory.createCompoundBorder(
        		BorderFactory.createEmptyBorder(8, 0, 20, 20),
        		BorderFactory.createTitledBorder("Color chosed")));
        
        tempPanel.add(sumPanel);
        tempPanel.add(colorPanel);
                		
        add( "West", chroma);
        
        add( "South", mixer );
        
        add( "Center", tempPanel);
        
        // Add the following function in case that you want to test the class
        // instead the tempPanel.
        //add("Center", testChomaCanvas());
        
        compColor.update();
    }
    
    /** Builds a panel with all the data that collected from the user */
    private JPanel addSumaryPanel(JPanel p){
    	
    	JPanel pMatPanel = new JPanel(new GridLayout(3,3)); 
    	  	
    	Dimension d = new Dimension(36,22);
    	for(int i=0; i<3; i++){
			for(int j=0; j<3; j++){
				pMatField[i][j] = new JTextField(/*i + */" ? "/* + j*/);
				pMatField[i][j].setSize(d);
				pMatField[i][j].setMaximumSize(d);
				pMatField[i][j].setMinimumSize(d);
				pMatField[i][j].setPreferredSize(d);
				
				pMatPanel.add(pMatField[i][j]);
			}
		}
    	   	
    	JPanel allTogether = new JPanel(new GridLayout(1,2));
    	
    	   	
    	JPanel temp = new JPanel(new BorderLayout());
    	JLabel text = new JLabel("        Primaries");
    	temp.add(text, BorderLayout.PAGE_START);
    	temp.add(pMatPanel, BorderLayout.CENTER);
    	
    	    	
    	allTogether.add(temp);
    	
    	temp = new JPanel(new BorderLayout());
    	text = new JLabel("            Gamma");
    	temp.add(text,BorderLayout.PAGE_START);
    	temp.add(gamma,BorderLayout.PAGE_END);
    	
    	allTogether.add(temp);
    	
    	p.add(allTogether,BorderLayout.LINE_START);
    	
    	return p;
    	
    }
   
    /** Updates the data on this panel */
    public void updateData(double[][] pData, double g){
    	for(int i=0; i<3; i++){
			for(int j = 0; j<3; j++){
				
				pMatField[i][j].setText(doubleToString(pData[i][j],3));
			}
		}
    	gamma.setText(Double.toString(g));
    }
    
    /** Converts a double to string with given number of digits after the dot */
    private String doubleToString(double val, int digits){
		char sign = (val < 0.0) ? '-' : ' ';
		double aval = Math.abs(val);
		int whole = (int)aval;
		double frac = aval - (double)whole;
		int digit;
		String res;
		
		res = sign + Integer.toString(whole) + ".";
		for (int i=0; i< digits; i++) {
			frac *= 10.0;
			digit = (int)frac;
			frac = frac - (double)digit;
			res += Integer.toString(digit);
		}
		
		return res;
	}
    
    /** Resets the gamut point counter */
    public void resetGammutPoints(){
    	chroma.resetGamutCnt();
    }
    
    /** Activated when test mode is on */
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == testButton){
            if(chroma.setGamutPoint(Integer.parseInt(testTextField_x.getText()), 
                    300-Integer.parseInt(testTextField_y.getText())) == false){
                System.out.println(" the (xx,yy) out of range.! ");
            }
            return;
        }
    }
    
    /** A simple UI component to test the coordinates of the spectrum chart */
    public JLabel testChomaCanvas(){
        testLabel = new JLabel();
        testLabel.setLayout(new GridLayout(2,2));
        
        testButton = new JButton("Compute");
        testButton.addActionListener(this);
        
        testTextField_x = new JTextField("x");
        testTextField_y = new JTextField("y");
        testLabel.setSize(70,60);
        
        testLabel.add(testTextField_x);
        testLabel.add(testTextField_y);
        testLabel.add(testButton);
        
        return testLabel;
    }
    
    /** Draw the Primaries values */
    public void drawPrimaries(double X, double Y, double Z){
        
        double x,xx;
        double y,yy;
        
        x = X/(X+Y+Z);
        y = Y/(X+Y+Z);
        
        // Print for testing 
        System.out.println("x = " + x + "; y = " + y );
        
        // Check for some unusual cases
        if(y<0) 
            y = y*(-1);
        if(x<0) 
            x = x*(-1);
        if(x>1) 
            x = x - (x-1) - 0.28;
        if(y>1)
            y = y - (y-1) - 0.01;
        
        System.out.println("x = " + x + "; y = " + y );
        
        xx = x*300;
        yy = (1-y)*300;
        
        System.out.println("xx = " + xx + "; yy = " + yy );
        
        if(xx>210) 
            xx=210;
        if(yy>279)
            yy=279;
        
        if(chroma.setGamutPoint((int)xx, ((int)yy)) == false){
            System.out.println(" the (xx,yy) out of range.! ");
        }
        
        compColor.update();
        chroma.repaint();
    }
}
